package me.zhengjie.modules.web.service.pay.order;

import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.domain.ExamOrder;
import me.zhengjie.modules.system.domain.ExamResult;
import me.zhengjie.modules.system.service.AtmrService;
import me.zhengjie.modules.system.service.ExamOrderService;
import me.zhengjie.modules.system.service.dto.AtmrDto;
import me.zhengjie.modules.system.service.dto.ExamOrderDto;
import me.zhengjie.modules.web.domain.pay.OrderSubmitRequest;
import me.zhengjie.modules.web.domain.pay.OrderSubmitResponse;
import me.zhengjie.modules.web.domain.test.VerifyGroupTestRespDTO;
import me.zhengjie.modules.web.enums.ExamOrderTypeEnum;
import me.zhengjie.modules.web.enums.OrderStatusEnum;
import me.zhengjie.modules.web.service.ITestingService;
import me.zhengjie.modules.web.service.mybatisplus.IExamResultPlusService;
import me.zhengjie.modules.web.service.order.IOrderService;
import me.zhengjie.modules.web.service.pay.IOrderSubmitService;
import me.zhengjie.modules.weixin.utils.OrderUtil;
import me.zhengjie.request.ATMRContext;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.enums.OrderPrefixEnum;
import me.zhengjie.utils.enums.OrderTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Objects;

@Service
public class ExamReportOrderSubmitServiceImpl implements IOrderSubmitService {
    @Autowired
    private ExamOrderService examOrderService;

    @Autowired
    private AtmrService atmrService;

    @Autowired
    private ITestingService testingService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IExamResultPlusService examResultPlusService;

    @Override
    public int orderType() {
        return OrderTypeEnum.Report.getOrderType();
    }

    @Override
    public OrderSubmitResponse execute(OrderSubmitRequest request) {
        if (StringUtils.isBlank(request.getAtmrId())){
            throw new WeChatException("缺少参数atmrId");
        }
        AtmrDto atmrDto = atmrService.findById(Integer.valueOf(request.getAtmrId()));
        if (Objects.isNull(atmrDto)){
            throw new WeChatException("参数传递有误");
        }
        BigDecimal orderAmount = new BigDecimal(atmrDto.getReportPrice());
        Integer groupId = request.getGroupId();
        if (Objects.nonNull(groupId)){
            VerifyGroupTestRespDTO respDTO = testingService.verifyGroupTesting(groupId,request.getGroupCode());
            if (Objects.nonNull(respDTO) && respDTO.isCanBeUsed()){
                orderAmount = respDTO.getReportPrice();
            }
        }else {
            if(Objects.nonNull(request.getResultId())){
                ExamResult examResult = examResultPlusService.getById(request.getResultId());
                if (Objects.nonNull(examResult)){
                    orderAmount = examResult.getReportPrice();
                }
            }
        }
        String orderSn = OrderUtil.GetOrderNumber(OrderPrefixEnum.Report.getPrefix());
        ExamOrder examOrder = new ExamOrder();
        examOrder.setOrderAmount(orderAmount);
        examOrder.setAtmrId(request.getAtmrId());
        examOrder.setOpenId(ATMRContext.getContext().getOpenId());
        examOrder.setOrderNo(orderSn);
        examOrder.setTeamTestid(groupId);
        examOrder.setBeAnswer(1);
        examOrder.setCreateTime(DateUtil.TimestampNow());
        examOrder.setPayStatus(OrderStatusEnum.WaitingPay.getOrderStatus());
        examOrder.setOrderStatus(OrderStatusEnum.WaitingPay.getOrderStatus());
        examOrder.setPayAmount(BigDecimal.ZERO);
        examOrder.setDiscountAmount(BigDecimal.ZERO);
        examOrder.setPaySerialid("");
        examOrder.setPrePayid("");
        examOrder.setOrderType(ExamOrderTypeEnum.Report.getExamOrderType());
        examOrder.setResultId(request.getResultId());
        Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now().plusMinutes(ConstantUtils.DefaultExpireTime));
        examOrder.setExpireTime(timestamp);
        if (orderAmount.compareTo(BigDecimal.ZERO) == 0){
            examOrder.setPayStatus(OrderStatusEnum.Payed.getOrderStatus());
            examOrder.setOrderStatus(OrderStatusEnum.Payed.getOrderStatus());
        }
        ExamOrderDto examOrderDto = examOrderService.create(examOrder);
        OrderSubmitResponse response = new OrderSubmitResponse();
        response.setOrderId(examOrderDto.getOrderId());
        response.setOrderSn(orderSn);
        response.setOrderStatus(examOrder.getOrderStatus());

        orderService.orderExpire2Redis(orderType(),orderSn,timestamp);
        return response;
    }
}
